﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace StrategyGame
{
    class CityScreen
	{
		#region Static resources

		static SpriteFont HeaderFont;
        static SpriteFont SubHeaderFont;
        static SpriteFont BodyFont;

        static Texture2D LeftArrowTexture;
        static Texture2D RightArrowTexture;

        static Texture2D NoProjectTexture;

        public static void LoadContent(ContentManager content)
        {
            HeaderFont = content.Load<SpriteFont>("fonts/LargeFont");
            SubHeaderFont = content.Load<SpriteFont>("fonts/MediumFont");
            BodyFont = content.Load<SpriteFont>("fonts/SmallFont");

            LeftArrowTexture = content.Load<Texture2D>("ui/interface_leftarrow");
            RightArrowTexture = content.Load<Texture2D>("ui/interface_rightarrow");

            NoProjectTexture = content.Load<Texture2D>("ui/ng");
        }

		#endregion

		#region Rendering

		public void Draw(SpriteBatch sprites, Rectangle draw_area, Nation player_nation)
        {
            Rectangle header_bar = new Rectangle();
            header_bar.Width = draw_area.Width;
            header_bar.Height = 100;
            header_bar.X = 0;
            header_bar.Y = 0;
            DrawHeaderBar(sprites, player_nation, header_bar);

            //left-hand bar
            Rectangle left_bar_rect = new Rectangle();
            left_bar_rect.X = draw_area.Left;
            left_bar_rect.Width = 200;
            left_bar_rect.Y = header_bar.Bottom;
            left_bar_rect.Height = draw_area.Height - left_bar_rect.Y;
            DrawLeftInfoBar(sprites, player_nation, left_bar_rect);

            //left-hand info bar
            Rectangle right_bar_rect = new Rectangle();
            right_bar_rect.Width = 200;
            right_bar_rect.X = draw_area.Right - right_bar_rect.Width;
            right_bar_rect.Y = header_bar.Bottom;
            right_bar_rect.Height = draw_area.Height - right_bar_rect.Y;
            DrawRightInfoBar(sprites, player_nation, right_bar_rect);
        }

        private void DrawHeaderBar(SpriteBatch sprites, Nation player_nation, Rectangle bar_rect)
        {
            SpriteRectangle.Draw(sprites, bar_rect, player_nation.DarkerNationalColor);

            string cityname_text;

            if (player_nation.ActiveCity != null)
                cityname_text = player_nation.ActiveCity.Name + ": Level " + player_nation.ActiveCity.CityLevel.ToString() + " " + City.PopulationToString(player_nation.ActiveCity.Population);
            else
                cityname_text = "No city selected";

            Rectangle city_name_label_rect = new Rectangle(bar_rect.X, bar_rect.Y + 20, bar_rect.Width, 40);
            Vector2 city_name_pos = TextUtils.GetPositionForAlignedText(HeaderFont, cityname_text, city_name_label_rect, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Middle);

            TextUtils.DrawWithDropShadow(sprites, HeaderFont, cityname_text, city_name_pos, Color.White, Color.Black, 1);

            string city_subheader_label = "Population: " + player_nation.ActiveCity.Population.ToString();
            Rectangle city_subheader_rect = new Rectangle(bar_rect.X, city_name_label_rect.Bottom + 4, bar_rect.Width, 40);

            Vector2 city_subheader_pos = TextUtils.GetPositionForAlignedText(SubHeaderFont, city_subheader_label, city_subheader_rect, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Top);
            TextUtils.DrawWithDropShadow(sprites, SubHeaderFont, city_subheader_label, city_subheader_pos, Color.White, Color.Black, 1);
        }

        private void DrawLeftInfoBar(SpriteBatch sprites, Nation player_nation, Rectangle bar_rect)
        {
            City city = player_nation.ActiveCity;

            SpriteRectangle.Draw(sprites, bar_rect, new Color(20, 20, 20, 255));// player_nation.DarkerNationalColor);

            List<string> info_text = new List<string>();
            info_text.Add("Population: " + city.Population.ToString());
            info_text.Add("Living Space: " + city.LivingSpace.ToString());
            info_text.Add("Food :" + city.Food.ToString());
            info_text.Add("Culture: " + city.Culture.ToString());
            info_text.Add(String.Empty);
            info_text.Add("Growth Rate: +" + city.CalcuateGrowth().ToString());
            info_text.Add(String.Empty);
            info_text.Add("Free Workers: " + city.FreeWorkers.ToString());
            info_text.Add("Hired for construction: " + city.ManpowerPerTurn.ToString());
            info_text.Add("...by private sector: " + city.PrivateManpowerPerTurn.ToString());
            info_text.Add("...by government: " + city.GovernmentManpowerPerTurn.ToString());


            int inner_padding = 8;

            Rectangle header_rect = new Rectangle();
            header_rect.Y = bar_rect.Y + inner_padding;
            header_rect.X = bar_rect.X + inner_padding;
            header_rect.Width = bar_rect.Width - (inner_padding * 2);
            header_rect.Height = 40;

            string header_text = "City Information";
            Vector2 header_text_pos = TextUtils.GetPositionForAlignedText(SubHeaderFont, header_text, header_rect, TextUtils.HorizontalAlignment.Right, TextUtils.VerticalAlignment.Bottom);
            TextUtils.DrawWithDropShadow(sprites, SubHeaderFont, header_text, header_text_pos, Color.White, Color.Black, 1);

            Rectangle inner_rect = new Rectangle(bar_rect.X, bar_rect.Y, bar_rect.Width, bar_rect.Height);
            inner_rect.Inflate(-inner_padding, -inner_padding);
            inner_rect.Height -= header_rect.Height + inner_padding;
            inner_rect.Y += header_rect.Height + inner_padding;

            TextUtils.DrawMultipleAlignedLines(sprites, BodyFont, Color.White, info_text.ToArray(), inner_rect, TextUtils.HorizontalAlignment.Right, TextUtils.VerticalAlignment.Top);
        }

        private void DrawRightInfoBar(SpriteBatch sprites, Nation player_nation, Rectangle bar_rect)
        {
            City city = player_nation.ActiveCity;

            SpriteRectangle.Draw(sprites, bar_rect, new Color(20, 20, 20, 255));//player_nation.DarkerNationalColor);

            int inner_padding = 8;

            Rectangle header_rect = new Rectangle();
            header_rect.Y = bar_rect.Y + inner_padding;
            header_rect.X = bar_rect.X + inner_padding;
            header_rect.Width = bar_rect.Width - (inner_padding * 2);
            header_rect.Height = 40;

            Rectangle inner_rect = new Rectangle(bar_rect.X, bar_rect.Y, bar_rect.Width, bar_rect.Height);
            inner_rect.Inflate(-inner_padding, -inner_padding);
            inner_rect.Height = 275;
            inner_rect.Y = header_rect.Bottom + inner_padding;

            string header_text = "Construction Project";
            Vector2 header_text_pos = TextUtils.GetPositionForAlignedText(SubHeaderFont, header_text, header_rect, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Bottom);
            TextUtils.DrawWithDropShadow(sprites, SubHeaderFont, header_text, header_text_pos, Color.White, Color.Black, 1);

            Rectangle leftarrow_rect = new Rectangle();
            leftarrow_rect.Width = 32;
            leftarrow_rect.Height = 64;
            leftarrow_rect.Y = header_rect.Bottom + inner_padding;
            leftarrow_rect.X = inner_rect.X;

            sprites.Draw(LeftArrowTexture, leftarrow_rect, Color.Silver);

            Rectangle rightarrow_rect = new Rectangle();
            rightarrow_rect.Width = 32;
            rightarrow_rect.Height = 64;
            rightarrow_rect.Y = header_rect.Bottom + inner_padding;
            rightarrow_rect.X = inner_rect.Right - rightarrow_rect.Width;

            sprites.Draw(RightArrowTexture, rightarrow_rect, Color.Silver);

            int icon_size = 64;
            Rectangle project_icon_rect = new Rectangle();
            project_icon_rect.X = bar_rect.X + (bar_rect.Width / 2 - icon_size/2);
            project_icon_rect.Y = rightarrow_rect.Y;
            project_icon_rect.Width = icon_size;
            project_icon_rect.Height = icon_size;

            Rectangle project_name_rect = new Rectangle(inner_rect.X, project_icon_rect.Bottom + inner_padding, inner_rect.Width, 20);
            Texture2D project_icon;
            string project_name;

            if (city.CurrentProject != null)
            {
                project_icon = city.CurrentProject.ProjectIcon;

                project_name = city.CurrentProject.ProjectName;

                project_name += "  (" + city.ManpowerSpentOnProject + "/" + city.CurrentProject.ManpowerRequired + ")";
            }
            else
            {
                project_icon = NoProjectTexture;
                project_name = "Nothing";
            }

            sprites.Draw(project_icon, project_icon_rect, Color.White);

            Vector2 project_name_pos = TextUtils.GetPositionForAlignedText(BodyFont, project_name, project_name_rect, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Top);
            sprites.DrawString(BodyFont, project_name, project_name_pos, Color.White);

            if (city.CurrentProject != null)
            {
                Rectangle construction_problem_info = new Rectangle(project_name_rect.X, project_name_rect.Bottom, project_name_rect.Width, 40);
                string[] problems = city.CurrentProject.GetProblemsBuildingThis(city);

                if (problems != null)
                {
                    TextUtils.DrawMultipleAlignedLines(sprites, BodyFont, Color.Red, problems, construction_problem_info, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Top);
                }

                Rectangle project_info_rect = new Rectangle(construction_problem_info.X, construction_problem_info.Bottom + inner_padding, construction_problem_info.Width, construction_problem_info.Height);
                string[] project_info = city.CurrentProject.InfoText;

                if (project_info != null)
                {
                    TextUtils.DrawMultipleAlignedLines(sprites, BodyFont, Color.White, project_info, project_info_rect, TextUtils.HorizontalAlignment.Middle, TextUtils.VerticalAlignment.Top);
                }
            }
        }

		#endregion

		#region Input

		public void KeyPressed(Keys key, bool just_pressed, StrategyGame game)
        {
            if (just_pressed)
            {
                switch (key)
                {
                    case Keys.Tab:
                        game.PlayerNation.NextCity();
                        game.CenterCameraOn(game.PlayerNation.ActiveCity);
                        break;

                    case Keys.Left:
                        if (game.PlayerNation.ActiveCity != null)
                            game.PlayerNation.ActiveCity.SelectProject(City.ProjectSelectOption.Back);
                        break;
                    case Keys.Right:
                        if (game.PlayerNation.ActiveCity != null)
                            game.PlayerNation.ActiveCity.SelectProject(City.ProjectSelectOption.Forward);
                        break;
                    case Keys.Delete:
                        if (game.PlayerNation.ActiveCity != null)
                            game.PlayerNation.ActiveCity.SelectProject(City.ProjectSelectOption.Cancel);
                        break;
                }
            }
		}

		#endregion
	}
}
